package com.seguriapp.complaints.gwt.client.activity;

import java.util.List;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.seguriapp.common.gwt.client.AbstractSeguriAppActivity;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppCallback;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.complaints.gwt.client.place.ComplaintsListPlace;
import com.seguriapp.complaints.gwt.shared.ChangeComplaintVisibilityAction;
import com.seguriapp.complaints.gwt.shared.ChangeComplaintVisibilityResult;
import com.seguriapp.complaints.gwt.shared.GetComplaintMediaDataAction;
import com.seguriapp.complaints.gwt.shared.GetComplaintMediaDataResult;
import com.seguriapp.entities.Complaint;
import com.seguriapp.entities.MediaContent;

public class ComplaintDetailsActivity extends AbstractSeguriAppActivity {

  private final Display display;
  private List<MediaContent> mediaDataList;
  private Long complaintId;

  public ComplaintDetailsActivity(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController,
      final Display display, final AbstractSeguriAppPlace place, final SeguriAppClientSession clientSession) {
    super(dispatchAsync, eventBus, placeController, place, clientSession);
    display.setPresenter(this);
    this.display = display;
  }

  @Override
  protected void onStart(final AcceptsOneWidget widget, final EventBus eventBus) {
    widget.setWidget(display.asWidget());
    final Object complaint = clientSession.get(SeguriAppClientSession.Key.Complaint);
    if (complaint != null && complaint instanceof Complaint) {
      complaintId = ((Complaint) complaint).getId();
      getComplaintDetails((Complaint) complaint);
      getComplaintMediaData(complaintId);
    } else {
      display.setNoDataMessage();
    }
  }

  private void getComplaintDetails(final Complaint complaint) {
    display.setData(complaint);
  }

  private void getComplaintMediaData(final Long complaintId) {
    final GetComplaintMediaDataAction action = new GetComplaintMediaDataAction();
    action.setComplaintId(complaintId);
    dispatchAsync.execute(action, new SeguriAppCallback<GetComplaintMediaDataResult>() {

      public void onSuccess(final GetComplaintMediaDataResult result) {
        mediaDataList = result.getMediaDataList();
        if (mediaDataList != null && !mediaDataList.isEmpty()) {
          display.setMediaData(mediaDataList, result.getAudioUrl(), result.getImageUrl(), result.getVideoUrl(), result.getAudioValidExt(),
              result.getVideoValidExt());
        } else {
          display.showNoMediaDataMessage();
        }
      }

      public void onFailure(Throwable e) {
        Window.alert("Error: " + e.getMessage());
      }
    });

  }

  public void changeComplaintVisibility(boolean isVisible) {
    final ChangeComplaintVisibilityAction action = new ChangeComplaintVisibilityAction();
    action.setComplaintId(complaintId);
    action.setMapVisibility(isVisible);
    dispatchAsync.execute(action, new SeguriAppCallback<ChangeComplaintVisibilityResult>() {

      public void onSuccess(final ChangeComplaintVisibilityResult result) {
        if (result.isSuccess()) {
          display.changeVisibilityLinkText(result.isMapVisibility());
        } else {
          display.showVisibilityErrorMsg();
        }
      }

      public void onFailure(Throwable e) {
        Window.alert("Error: " + e.getMessage());
      }
    });
  }

  public void goBackToList() {
    final Place place = new ComplaintsListPlace("");
    placeController.goTo(place);
  }

  public interface Display extends SeguriAppDisplay {

    /**
     * setea el presenter del activity
     */
    void setPresenter(ComplaintDetailsActivity presenter);

    void showVisibilityErrorMsg();

    void changeVisibilityLinkText(boolean isVisible);

    void setMediaData(List<MediaContent> mediaDataList, String audioUrl, String imageUrl, String videoUrl, String audioValidExt, String videoValidExt);

    void showNoMediaDataMessage();

    void setNoDataMessage();

    void setData(Complaint complaint);
  }

}
